﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Toolshankswith7_24taperforautomatictoolchanger1
    {
        public static void _Toolshankswith7_24taperforautomatictoolchanger1(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);


            double scale = 1; //放大系数


            double d1 = Convert.ToDouble(data["d1"]) * scale;
            double d2 = Convert.ToDouble(data["dcon"]) * scale;
            double d3 = Convert.ToDouble(data["df"]) * scale;
            double d4 = Convert.ToDouble(data["dn"]) * scale;
            double d5 = Convert.ToDouble(data["cnd"]) * scale;
            double d7 = Convert.ToDouble(data["crks"]) * scale;
            double l1 = Convert.ToDouble(data["oal"]) * scale;
            double l2 = Convert.ToDouble(data["lpr"]) * scale;
            double l3 = Convert.ToDouble(data["lf"]) * scale;
            double l4 = l1 - l2;
            double l5 = Convert.ToDouble(data["flgt"]) * scale;
            double l6 = Convert.ToDouble(data["l6"]) * scale;
            double l12 = Convert.ToDouble(data["thlgth"]) * scale;
            double s = Convert.ToDouble(data["drvs"]) * scale;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l1 + 10, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 5, v1.Y);
            Vector2 v5 = new Vector2(v3.X + 1.5, v3.Y + d1 / 2);
            Vector2 v4 = new Vector2(v3.X, v5.Y - 1.5 * Math.Tan(30 * pi / 180));
            Vector2 v6 = new Vector2(v5.X + l2 - l3 - 2, v5.Y);
            Vector2 v7 = fun.Polar(v6, -75 * pi / 180, (d1 - d4) / 2 / Math.Cos(15 * pi / 180));
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v13 = new Vector2(v6.X + l3 - l5, v3.Y + d3 / 2);
            Vector2 v12 = new Vector2(v13.X - (v13.Y - v7.Y), v7.Y);
            Vector2 v14 = new Vector2(v13.X + l5, v13.Y);
            Vector2 v15 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v16 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v17 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v18 = new Vector2(v14.X, v3.Y + d4 / 2 - 0.5);
            Vector2 v19 = new Vector2(v14.X, v18.Y * -1);
            Vector2 v20 = new Vector2(v16.X, v19.Y);
            Vector2 v21 = new Vector2(v13.X, v18.Y);
            Vector2 v22 = new Vector2(v14.X, v3.Y + d2 / 2);
            Vector2 v23 = new Vector2(v22.X + 1.5, v22.Y);
            Vector2 v24 = new Vector2(v23.X, v23.Y * -1);
            Vector2 v25 = new Vector2(v15.X, v22.Y * -1);
            Vector2 v26 = new Vector2(v22.X + l6, v22.Y);
            Vector2 v27 = new Vector2(v26.X + 1, v26.Y - 1);
            Vector2 v28 = new Vector2(v27.X, v27.Y * -1);
            Vector2 v29 = new Vector2(v26.X, v26.Y * -1);
            Vector2 v32 = new Vector2(v14.X + l4 - l12, v3.Y + d7 / 2);
            Vector2 v30 = new Vector2(v32.X - 1.5, v27.Y);
            Vector2 v31 = new Vector2(v30.X, v30.Y * - 1);
            Vector2 v34 = new Vector2(v32.X + l12, v30.Y - 0.5);
            Vector2 v33 = new Vector2(v34.X - 1.5, v32.Y);
            Vector2 v35 = new Vector2(v34.X, v34.Y * -1);
            Vector2 v36 = new Vector2(v33.X, v33.Y * -1);
            Vector2 v37 = new Vector2(v32.X, v32.Y * -1);
            Vector2 v38 = new Vector2(v33.X + 1.5 * (v32.Y - v30.Y) / (v33.Y - v34.Y), v30.Y);
            Vector2 v39 = new Vector2(v38.X, v38.Y * -1);
            Vector2 v40 = new Vector2(v3.X, v3.Y + d5 / 2);
            Vector2 v41 = new Vector2(v34.X, v40.Y);
            Vector2 v42 = new Vector2(v40.X, v3.Y - d5 / 2);
            Vector2 v43 = new Vector2(v41.X, v42.Y);

            Vector2 v44 = new Vector2(v2.X + l1, v2.Y);
            Vector2 v45 = new Vector2(v44.X - d1 / 2 - 5, v44.Y);
            Vector2 v46 = new Vector2(v44.X + d1 / 2 + 5, v44.Y);
            Vector2 v47 = new Vector2(v44.X, v44.Y + d3 / 2 + 5);
            Vector2 v48 = new Vector2(v44.X, v45.Y - d3 / 2 - 5);
            Vector2 v49 = new Vector2(v44.X - d1 / 2, v18.Y);
            Vector2 v51 = new Vector2(v44.X + d1 / 2, v49.Y);
            Vector2 v50 = new Vector2(v49.X, v49.Y * -1);
            Vector2 v52 = new Vector2(v51.X, v50.Y);
            Vector2 v53 = new Vector2(v44.X, v44.Y + d3 / 2);
            Vector2 v54 = new Vector2(v44.X, v44.Y - d3 / 2);




            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line2 = new Line(v40, v41);
            line2.Layer = refin;
            line2.Linetype = Linetype.Dashed;
            line2.LinetypeScale = 5;
            dxf.AddEntity(line2);

            Line line3 = new Line(v42, v43);
            line3.Layer = refin;
            line3.Linetype = Linetype.Dashed;
            line3.LinetypeScale = 5;
            dxf.AddEntity(line3);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v5));
            line4.Vertexes.Add(new LwPolylineVertex(v10));
            line4.Vertexes.Add(new LwPolylineVertex(v9));
            line4.Vertexes.Add(new LwPolylineVertex(v6));
            line4.Vertexes.Add(new LwPolylineVertex(v5));
            line4.Vertexes.Add(new LwPolylineVertex(v4));
            line4.Vertexes.Add(new LwPolylineVertex(v11));
            line4.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line4, 0.3, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v6));
            line5.Vertexes.Add(new LwPolylineVertex(v7));
            line5.Vertexes.Add(new LwPolylineVertex(v8));
            line5.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, line5, 0.3, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v7));
            line6.Vertexes.Add(new LwPolylineVertex(v12));
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line6, 0.3, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v12));
            line7.Vertexes.Add(new LwPolylineVertex(v13));
            line7.Vertexes.Add(new LwPolylineVertex(v14));
            line7.Vertexes.Add(new LwPolylineVertex(v15));
            line7.Vertexes.Add(new LwPolylineVertex(v16));
            line7.Vertexes.Add(new LwPolylineVertex(v17));
            fun.PolyFill(dxf, line7, 0.3, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v13));
            line8.Vertexes.Add(new LwPolylineVertex(v21));
            line8.Vertexes.Add(new LwPolylineVertex(v18));
            line8.Vertexes.Add(new LwPolylineVertex(v20));
            line8.Vertexes.Add(new LwPolylineVertex(v19));
            line8.Vertexes.Add(new LwPolylineVertex(v21));
            line8.Vertexes.Add(new LwPolylineVertex(v20));
            line8.Vertexes.Add(new LwPolylineVertex(v16));
            line8.Vertexes[5].Bulge = Math.Tan(97 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.3, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v22));
            line9.Vertexes.Add(new LwPolylineVertex(v23));
            line9.Vertexes.Add(new LwPolylineVertex(v24));
            line9.Vertexes.Add(new LwPolylineVertex(v25));
            line9.Vertexes[0].Bulge = Math.Tan(73 * pi / 180 / 4);
            line9.Vertexes[2].Bulge = Math.Tan(73 * pi / 180 / 4);
            fun.PolyFill(dxf, line9, 0.3, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v23));
            line10.Vertexes.Add(new LwPolylineVertex(v26));
            line10.Vertexes.Add(new LwPolylineVertex(v29));
            line10.Vertexes.Add(new LwPolylineVertex(v24));
            fun.PolyFill(dxf, line10, 0.3, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v27));
            line11.Vertexes.Add(new LwPolylineVertex(v30));
            line11.Vertexes.Add(new LwPolylineVertex(v31));
            line11.Vertexes.Add(new LwPolylineVertex(v28));
            fun.PolyFill(dxf, line11, 0.3, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v30));
            line12.Vertexes.Add(new LwPolylineVertex(v32));
            line12.Vertexes.Add(new LwPolylineVertex(v37));
            line12.Vertexes.Add(new LwPolylineVertex(v31));
            fun.PolyFill(dxf, line12, 0.3, contour);
             
            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v32));
            line13.Vertexes.Add(new LwPolylineVertex(v33));
            line13.Vertexes.Add(new LwPolylineVertex(v36));
            line13.Vertexes.Add(new LwPolylineVertex(v37));
            fun.PolyFill(dxf, line13, 0.3, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v33));
            line14.Vertexes.Add(new LwPolylineVertex(v34));
            line14.Vertexes.Add(new LwPolylineVertex(v35));
            line14.Vertexes.Add(new LwPolylineVertex(v36));
            fun.PolyFill(dxf, line14, 0.3, contour);

            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v38));
            line15.Vertexes.Add(new LwPolylineVertex(v30));
            line15.Vertexes.Add(new LwPolylineVertex(v31));
            line15.Vertexes.Add(new LwPolylineVertex(v39));
            fun.PolyFill(dxf, line15, 0.15, contour);

            LwPolyline line16 = new LwPolyline();
            line16.Vertexes.Add(new LwPolylineVertex(v26));
            line16.Vertexes.Add(new LwPolylineVertex(v27));
            line16.Vertexes.Add(new LwPolylineVertex(v28));
            line16.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, line16, 0.15, contour);

            fun.drawcircle(dxf, v44, d5 / 2, 0.3, contour);

            fun.drawcircle(dxf, v44, d1 / 2, 0.3, contour);

            fun.drawcircle(dxf, v44, (v4.Y - v3.Y), 0.3, contour);

            LwPolyline line17 = new LwPolyline();
            line17.Vertexes.Add(new LwPolylineVertex(v50));
            line17.Vertexes.Add(new LwPolylineVertex(v49));
            line17.Vertexes.Add(new LwPolylineVertex(v53));
            line17.Vertexes.Add(new LwPolylineVertex(v51));
            line17.Vertexes.Add(new LwPolylineVertex(v52));
            line17.Vertexes.Add(new LwPolylineVertex(v54));
            line17.Vertexes.Add(new LwPolylineVertex(v50));
            line17.Vertexes[1].Bulge = Math.Tan(-57 * pi / 180 / 4);
            line17.Vertexes[2].Bulge = Math.Tan(-57 * pi / 180 / 4);
            line17.Vertexes[4].Bulge = Math.Tan(-57 * pi / 180 / 4);
            line17.Vertexes[5].Bulge = Math.Tan(-57 * pi / 180 / 4);
            fun.PolyFill(dxf, line17, 0.15, contour);

            Line line18 = new Line(v45, v46);
            line18.Layer = centre;
            line18.Linetype = Linetype.DashDot;
            line18.LinetypeScale = 3;
            dxf.AddEntity(line18);

            Line line19 = new Line(v47, v48);
            line19.Layer = centre;
            line19.Linetype = Linetype.DashDot;
            line19.LinetypeScale = 3;
            dxf.AddEntity(line19);

            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 1;
            dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1;
            double offset = 10;


            //文本样式
            dimensionStyle.TextHeight = 2;

            double height = 2;
            double arrowsize = 1;
            double exline = 2;

            fun.dimrot1(dxf, v5, v10, 90, d1, 1, height, arrowsize, exline, "d1=" + d1, "", "");

            fun.dimrot1(dxf, v7, v8, 90, d1, 1, height, arrowsize, exline, "d4=" + d4, "", "");

            fun.dimrot1(dxf, v6, v14, 0, d1*2, 1, height, arrowsize, exline, "l3=" + l3, "", "");

            fun.dimrot1(dxf, v13, v14, 0, d4*2, 1, height, arrowsize, exline, "l5=" + l5, "", "");

            fun.dimrot1(dxf, v26, v29, -90, d4*3, 1, height, arrowsize, exline, "d2=" + d2, "", "");

            fun.dimrot1(dxf, v38, v35, -90, d4, 1, height, arrowsize, exline, "d7=" + d7, "", "");

            fun.dimrot1(dxf, v11, v10, 180, d4, 1, height, arrowsize, exline, "2", "", "");

            fun.dimrot1(dxf, v11, v15, 180, d1, 1, height, arrowsize, exline, "l2=" + l2, "", "");

            fun.dimrot1(dxf, v15, v35, 180, d1, 1, height, arrowsize, exline, "l4=" + l4, "", "");

            fun.dimrot1(dxf, v15, v29, 180, d4, 1, height, arrowsize, exline, "l6=" + l6, "", "");

            fun.dimrot1(dxf, v28, v29, 180, d4 * 2 / 3, 1, height, arrowsize, exline, "1*45°", "", "");

            fun.dimrot1(dxf, v27, v35, 180, d4*3/2, 1, height, arrowsize, exline, "l12=" + l12, "", "");

            fun.dimrot1(dxf, v11, v35, 180, d4 * 2, 1, height, arrowsize, exline, "l1=" + l1, "", "");

            fun.dimrot1(dxf, v53, v54, -90, d4*2, 1, height, arrowsize, exline, "d3=" + d3, "", "");

            fun.dimrot1(dxf, v50, v52, 180, d4, 1, height, arrowsize, exline, "S=" + s, "", "");

            fun.jdbz1(dxf, new Line(v6, fun.Polar(v6, 90 * pi / 180, 0.1)), new Line(v6, fun.Polar(v6, 105 * pi / 180, 0.1)), d4 * 4 + 2, "15°±15'", 1);

            fun.jdbz1(dxf, new Line(v13, fun.Polar(v13, 90 * pi / 180, 0.1)), new Line(v13, fun.Polar(v13, 45 * pi / 180, 0.1)), d4 , "45°", 1);

            fun.jdbz1(dxf, new Line(v10, fun.Polar(v10, 180 * pi / 180, 0.1)), new Line(v10, fun.Polar(v10, 150 * pi / 180, 0.1)), d4 * 2 / 3, "30°", 1);


            fun.gcbz(dxf, (fun.Polar(v5, 180 * pi / 180, d1)), 10, 90, 5,180, 8, 1, 1, "A", "0.02", 2, "ytd", 1);

            fun.gcbz(dxf, (fun.Polar(v6, 105 * pi / 180, d4 * 4)), 10, 15, 5, 0, 8, 1, 1, "A", "0.02", 2, "ytd", 1);

            fun.gcbz(dxf, (fun.Polar(v14, 90 * pi / 180, d1 * 2)), 10, 0, 5, 0, 8, 1, 1, "A", "0.02", 2, "ytd", 1);

            fun.gcbz(dxf, (fun.Polar(v33, 0 * pi / 180, d4)), 10, 90, 5, 0, 8, 1, 1, "A", "0.05", 2, "tzd", 1);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}